package com.demomaster.quickcompat.widget.base;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.demomaster.quickcompat.drawable.QuickBitmapDrawable;
import com.demomaster.quickcompat.drawable.QuickColorDrawable;
import com.demomaster.quickcompat.drawable.QuickDrawable;
import com.demomaster.quickcompat.drawable.QuickRoundDrawable;
import com.demomaster.quickcompat.drawable.QuickRoundRectDrawable;
import com.demomaster.quickcompat.drawable.QuickStateListDrawable;


public class QuickViewImpl<T extends View> {
    public String TAG = "QuickViewImpl";
    T delegateView;//代理实现
    public QuickViewAttribute attribute;

    public QuickViewImpl(T delegateView) {
        this.delegateView = delegateView;
    }

    public QuickViewImpl(T view, AttributeSet attrs, int defStyleAttr) {
        attribute = QuickViewUtil.handleCustomAttrs(view.getContext(), attrs, defStyleAttr);
        delegateView = view;
        Drawable background = view.getBackground();
        Drawable backgroundDrawable = null;
        //String desc = view.getContentDescription() + "";
        if (attribute != null) {
            //if (background instanceof StateListDrawable) {
            Drawable defDrawable = attribute.backgroundDrawable;
            if (defDrawable == null) {
                defDrawable = delegateView.getBackground();
            }

            float[] radii = QuickViewUtil.getCornerRadii(attribute);
            if (radii != null) {
                if (defDrawable instanceof QuickDrawable) {
                    if (attribute.radius != 0) {
                        ((QuickDrawable) defDrawable).setCornerRadius(attribute.radius);
                    } else {
                        ((QuickDrawable) defDrawable).setCornerRadii(radii);
                    }
                    backgroundDrawable = defDrawable;
                } else if (defDrawable instanceof GradientDrawable) {
                    if (attribute.radius != 0) {
                        ((GradientDrawable) defDrawable).setCornerRadius(attribute.radius);
                    } else {
                        ((GradientDrawable) defDrawable).setCornerRadii(radii);
                    }
                    backgroundDrawable = defDrawable;
                } else if (defDrawable instanceof ColorDrawable) {
                    backgroundDrawable = new QuickColorDrawable((ColorDrawable) defDrawable);
                    if (attribute.radius != 0) {
                        ((QuickColorDrawable) backgroundDrawable).setCornerRadius(attribute.radius);
                    } else {
                        ((QuickColorDrawable) backgroundDrawable).setCornerRadii(radii);
                    }
                } else {
                    if (attribute.radius != 0) {
                        backgroundDrawable = new QuickRoundDrawable(defDrawable, attribute.radius);
                        ((QuickDrawable) backgroundDrawable).setCornerRadius(attribute.radius);
                    } else {
                        backgroundDrawable = new QuickRoundDrawable(defDrawable, attribute.radius);
                        ((QuickDrawable) backgroundDrawable).setCornerRadii(radii);
                    }
                }
            }
            if (attribute.borderWidth > 0 && attribute.borderColor != null) {
                if (backgroundDrawable instanceof QuickDrawable) {
                    ((QuickDrawable) backgroundDrawable).setCornerRadii(radii);
                    ((QuickDrawable) backgroundDrawable).setStroke(attribute.borderWidth, attribute.borderColor);
                } else if (backgroundDrawable instanceof GradientDrawable) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        ((GradientDrawable) backgroundDrawable).setStroke(attribute.borderWidth, attribute.borderColor);
                    }
                } else {
                    if (backgroundDrawable instanceof ColorDrawable) {
                        backgroundDrawable = new QuickColorDrawable((ColorDrawable) defDrawable);
                        ((QuickColorDrawable) backgroundDrawable).setStroke(attribute.borderWidth, attribute.borderColor);
                    } else if (backgroundDrawable == null) {
                        QuickStateListDrawable quickStateListDrawable = new QuickStateListDrawable();

                        QuickRoundRectDrawable bg_state_focused = new QuickRoundRectDrawable();
                        bg_state_focused.setCornerRadii(radii);
                        bg_state_focused.setStrokeData(attribute.borderWidth, attribute.borderColor);
                        //bg_state_focused.setBgData(attribute.borderColor,android.R.attr.state_focused);
                        //new ColorDrawable(attribute.borderColor.getColorForState(new int[]{android.R.attr.state_focused}, 0));
                        quickStateListDrawable.addState(new int[]{-android.R.attr.state_focused}, bg_state_focused);
                        QuickRoundRectDrawable bg_state_pressed = new QuickRoundRectDrawable();
                        bg_state_pressed.setCornerRadii(radii);
                        // new ColorDrawable(attribute.borderColor.getColorForState(new int[]{android.R.attr.state_pressed}, 0));
                        //bg_state_pressed.setBgData(attribute.borderColor, android.R.attr.state_pressed);
                        bg_state_pressed.setStrokeData(attribute.borderWidth, attribute.borderColor);
                        quickStateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, bg_state_pressed);
                        QuickRoundRectDrawable bg_state_selected = new QuickRoundRectDrawable();
                        bg_state_selected.setCornerRadii(radii);
                        //bg_state_selected.setBgData(attribute.borderColor, android.R.attr.state_pressed);
                        bg_state_selected.setStrokeData(attribute.borderWidth, attribute.borderColor);
                        //Drawable bg_selected = new ColorDrawable(attribute.borderColor.getColorForState(new int[]{android.R.attr.state_selected}, 0));
                        quickStateListDrawable.addState(new int[]{-android.R.attr.state_selected}, bg_state_selected);

                        QuickRoundRectDrawable bg_state_checked = new QuickRoundRectDrawable();
                        bg_state_checked.setCornerRadii(radii);
                        //bg_state_checked.setBgData(attribute.borderColor, android.R.attr.state_checked);
                        bg_state_checked.setStrokeData(attribute.borderWidth, attribute.borderColor);
                        //Drawable bg_checked = new ColorDrawable(attribute.borderColor.getColorForState(new int[]{android.R.attr.state_checked}, 0));
                        quickStateListDrawable.addState(new int[]{-android.R.attr.state_checked}, bg_state_checked);

                        QuickRoundRectDrawable bg_state_enabled = new QuickRoundRectDrawable();
                        bg_state_enabled.setCornerRadii(radii);
                        //bg_state_enabled.setBgData(attribute.borderColor, android.R.attr.state_enabled);
                        bg_state_enabled.setStrokeData(attribute.borderWidth, attribute.borderColor);
                        //Drawable bg_enabled = new ColorDrawable(attribute.borderColor.getColorForState(new int[]{android.R.attr.state_enabled}, 0));
                        quickStateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, bg_state_enabled);

                        QuickRoundRectDrawable bg_state_normal = new QuickRoundRectDrawable();
                        bg_state_normal.setStrokeData(attribute.borderWidth, attribute.borderColor);
                        //Drawable bg_normal = new ColorDrawable(attribute.borderColor.getColorForState(new int[]{android.R.attr.state_focused}, 0));
                        //Drawable bg_normal = new ColorDrawable();// dealDrawable(defDrawable);
                        bg_state_normal.setCornerRadii(radii);
                        quickStateListDrawable.addState(new int[]{}, bg_state_normal);//默认
                        backgroundDrawable = quickStateListDrawable;
                    }

                }

                if (backgroundDrawable == null) {
                    backgroundDrawable = defDrawable;
                }
            }
        } else {
            backgroundDrawable = background;// dealDrawable(background);
        }
        attribute.backgroundDrawable = backgroundDrawable;
        QuickViewUtil.setBackgroundKeepingPadding(delegateView,  attribute.backgroundDrawable);
        if (view instanceof TextView) {
            int drawableWidth = attribute.drawableWidth;
            int drawableHeight = attribute.drawableHeight;
            //设置drawble的大小
            Drawable[] drawables = ((TextView) view).getCompoundDrawables();
            //Drawable[] drawables = getCompoundDrawablesRelative();
            if (!isAllNull(drawables) && drawableWidth != -1 && drawableHeight != -1) {
                if (drawableWidth > 0) {
                    //Drawable[] drawables_new = new Drawable[]{drawables[0], drawables[1], drawables[2], drawables[3]};
                    for (int i = 0; i < drawables.length; i++) {
                        if (drawables[i] != null) {
                            drawables[i].setBounds(0, 0, drawableWidth, drawableHeight);
                        }
                    }
                    ((TextView) view).setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);//将改变了属性的drawable再重新设置回去
                    //setCompoundDrawablesRelative(drawables[0], drawables[1], drawables[2], drawables[3]);//将改变了属性的drawable再重新设置回去
                }
            }
            drawables = ((TextView) view).getCompoundDrawablesRelative();
            if (!isAllNull(drawables) && drawableWidth != -1 && drawableHeight != -1) {
                for (Drawable drawable : drawables) {
                    if (drawable != null) {
                        drawable.setBounds(0, 0, drawableWidth, drawableHeight);
                    }
                }
                ((TextView) view).setCompoundDrawablesRelative(drawables[0], drawables[1], drawables[2], drawables[3]);//将改变了属性的drawable再重新设置回去
            }
        }
    }

    public static boolean isAllNull(Object[] array) {
        if (array == null) {
            return true; // 数组本身为null，认为全部为空
        }
        for (Object obj : array) {
            if (obj != null) {
                return false; // 只要有一个元素不为null，就返回false
            }
        }
        return true; // 所有元素都为null
    }

//    public Drawable getBackground(Drawable background) {
//        QDLogger.println("getBackground=" + background);
//        //background = dealDrawable(background);
//        if (attribute != null && attribute.radius != 0) {
//            return QuickDrawableUtil.convertToRadiusDrawable(background, attribute.radius);
//        } else if (attribute != null) {
//            if (attribute.radiusLeftTop > 0 || attribute.radiusLeftBottom > 0 || attribute.radiusRightTop > 0 || attribute.radiusRightBottom > 0) {
//                float[] radii = new float[]{
//                        attribute.radiusLeftTop, attribute.radiusLeftTop,
//                        attribute.radiusRightTop, attribute.radiusRightTop,
//                        attribute.radiusRightBottom, attribute.radiusRightBottom,
//                        attribute.radiusLeftBottom, attribute.radiusLeftBottom
//                };
//                return QuickDrawableUtil.convertToRadiusDrawable(background, radii);
//            }
//        }
////        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
////            delegateView.setForeground(background);
////        }
//        return background;
//    }

    public Drawable getForeground(Drawable foreground) {
        Log.i(TAG, "getForeground=" + foreground);
        foreground = QuickDrawableUtil.convertToRadiusDrawable(foreground, attribute.radius);
        if (attribute != null && attribute.radius != 0) {
            return QuickDrawableUtil.convertToRadiusDrawable(foreground, attribute.radius);
        } else if (attribute != null) {
            if (attribute.radiusLeftTop > 0 || attribute.radiusLeftBottom > 0 || attribute.radiusRightTop > 0 || attribute.radiusRightBottom > 0) {
                float[] radii = new float[]{
                        attribute.radiusLeftTop, attribute.radiusLeftTop,
                        attribute.radiusRightTop, attribute.radiusRightTop,
                        attribute.radiusRightBottom, attribute.radiusRightBottom,
                        attribute.radiusLeftBottom, attribute.radiusLeftBottom
                };
                return QuickDrawableUtil.convertToRadiusDrawable(foreground, radii);
            }
        }
        return foreground;
    }

    //对背景处理 圆角
    public Drawable dealDrawable(Drawable background) {
        Log.i(TAG, "getBackground=" + background);
        Drawable drawable = null;
        if (attribute != null) {
            if (attribute.radius != 0) {
                drawable = QuickDrawableUtil.convertToRadiusDrawable(background, attribute.radius);
            } else {
                if (attribute.radiusLeftTop > 0 || attribute.radiusLeftBottom > 0 || attribute.radiusRightTop > 0 || attribute.radiusRightBottom > 0) {
                    float[] radii = new float[]{
                            attribute.radiusLeftTop, attribute.radiusLeftTop,
                            attribute.radiusRightTop, attribute.radiusRightTop,
                            attribute.radiusRightBottom, attribute.radiusRightBottom,
                            attribute.radiusLeftBottom, attribute.radiusLeftBottom
                    };
                    drawable = QuickDrawableUtil.convertToRadiusDrawable(background, radii);
                }
            }
        }
        if (drawable != null) {
            return drawable;
        }
        return background;
    }

}
